<template>
  <el-dialog :title="editId ? '编辑': '新增'"
             :visible="showDia"
             width="30%"
             @close="handleClose"
             @open="handleOpen">
    <el-form label-width="80px">
      <el-form-item label="学科名称">
        <el-select v-model="formData.subjectID"
                   style="width:80%"
                   placeholder="请选择学科">
          <el-option v-for="item in subjectList"
                     :key="item.id"
                     :label="item.subjectName"
                     :value="item.id
                     " />
        </el-select>
      </el-form-item>
      <el-form-item label="目录名称">
        <el-input v-model="formData.tagName"
                  style="width:80%"
                  placeholder="请输入目录名称" />
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button type=""
                 size="small"
                 @click="handleClose">取 消</el-button>
      <el-button type="primary"
                 size="small"
                 style="background-color: #409eff; color:#fff"
                 @click="handleAdd">确 定</el-button>
    </template>
  </el-dialog>
</template>

<script>
import { getSubjectListAPI, addTagAPI, getTagDetailAPI, putTagAPI } from '@/api/subject'
export default {
  name: 'DialogView',
  props: {
    showDia: {
      type: Boolean,
      default: false
    },
    editId: {
      type: Number,
      default: 0
    }
  },
  data () {
    return {
      formData: {
        /*  // subjectID: '',
        directoryName: '' */
      },
      subjectList: []
    }
  },
  methods: {
    // 关闭弹窗
    handleClose () {
      this.$emit('update:showDia', false)
    },
    // 打开弹窗
    async handleOpen () {
      if (this.editId) {
        const resp = await getTagDetailAPI(this.editId)
        // console.log(resp)
        this.formData = resp
      }
      const res = await getSubjectListAPI({
        page: 1,
        pagesize: 10000
      })
      console.log(res)
      this.subjectList = res.items
    },
    // 新增
    async handleAdd () {
      if (this.editId) {
        await putTagAPI(this.editId, this.formData)
        this.$message.success('修改成功')
        this.handleClose()
      } else {
        await addTagAPI(this.formData)
        this.handleClose()
        this.$message.success('添加成功')
      }
    }
  }
}
</script>

<style>
</style>
